﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GameEngine.Controller
{
    using PureMVC.Patterns;
    using PureMVC.Interfaces;
    using GameEngine.Model;
    using GameEngine.View.Components;
    using System.Collections.Generic;
    using GameEngine.Model.Data;

    public class ThingChangedCommand<MyIdType> : SimpleCommand
        where MyIdType : IComparable<MyIdType>
    {
        override public void Execute(INotification notification)
        {
            IThingDataProxy<MyIdType> thing = notification.Body as IThingDataProxy<MyIdType>;
            switch (notification.Type) {
                case IThingDataProxyConstants<MyIdType>.THINGCHANGEDPOSITION_NOTIFICATIONTYPE:
                    IWorldDataProxy<MyIdType> world = ModelFacade<MyIdType>.Instance.Engine.Game.CurrentLevel.Worlds[thing.WorldId];

                    //TODO update occupied according to size

                    world.Map.VO.Tiles[(int)thing.MapPosition.X][(int)thing.MapPosition.Y].Occupied = false;
                    if (world.ItemsMapPosition.ContainsKey(thing.MapPosition))
                        world.ItemsMapPosition[thing.MapPosition].Remove(thing.Id);

                    //ModelFacade<ItemVOId>.Instance.Engine.Game.CurrentLevel.Worlds[thing.WorldName].ItemsMapPosition.Remove((int)thing.MapPosition.X);
                    thing.MapPosition = new Point()
                    {
                        X = Math.Round(thing.Position.X,0),
                        Y = Math.Round(thing.Position.Y,0)
                    };
                    
                    world.Map.VO.Tiles[(int)thing.MapPosition.X][(int)thing.MapPosition.Y].Occupied = true;
                    //id ! exist
                    if (!world.ItemsMapPosition.ContainsKey(thing.MapPosition))
                        world.ItemsMapPosition.Add(thing.MapPosition,new List<IThingVOId<MyIdType>>());
                    world.ItemsMapPosition[thing.MapPosition].Add(thing.Id);
                    break;
            }
        }
    }
}
